package com.smart.sso.server.controller.admin;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smart.mvc.model.Result;
import com.smart.mvc.validator.Validator;
import com.smart.mvc.validator.annotation.ValidateParam;
import com.smart.sso.server.controller.common.BaseController;
import com.smart.sso.server.log.SystemControllerLog;
import com.smart.sso.server.service.AppService;
import com.smart.sso.server.service.RoleLogService;
import com.smart.sso.server.util.OperationUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author YM
 */
@Api(tags = "用户角色与日志查询关系管理")
@Controller
@RequestMapping("/admin/roleLog")
public class RoleLogController extends BaseController {

	@Resource
	private AppService appService;
	@Resource
	private RoleLogService roleLogService;
	
	@ApiOperation("初始页")
	@RequestMapping(method = RequestMethod.GET)
	public String edit(
			@ApiParam(value = "角色id", required = true) @ValidateParam({ Validator.NOT_BLANK }) Integer roleId, Model model) {
		model.addAttribute("roleId", roleId);
		model.addAttribute("appList", appService.findByAll(true));
		return "/admin/roleLog";
	}
	
	@ApiOperation("日志查询授权提交")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@SystemControllerLog(operationId = OperationUtils.operation_permission_log ,description = "日志授权")
	public @ResponseBody Result save(
			@ApiParam(value = "应用id", required = true) @ValidateParam({ Validator.NOT_BLANK }) Integer appId,
			@ApiParam(value = "角色id", required = true) @ValidateParam({ Validator.NOT_BLANK }) Integer roleId,
			@ApiParam(value = "日志ids") String permissionIds) {
		roleLogService.allocate(appId, roleId, getAjaxIds(permissionIds));
		return Result.createSuccessResult().setMessage("授权成功");
	}
}